---
title: "Lab 2 Code"
author: "Fabio Setti"
date: "Spring-2025"
output: html_document
---

## Today's Packages 🤗

```{r}
# Run Individual lines to install packages

install.packages("rio")
install.packages("psych")
install.packages("tidyverse")
install.packages("car")
```

```{r}
library(car)
library(rio)
library(psych)
library(tidyverse)
theme_set(theme_classic(base_size = 14, 
                        base_family = 'serif'))
```


```{r}
GPA <- import("https://github.com/quinix45/PSYC-7804-Regression-Lab-Slides/raw/refs/heads/main/Slides%20Files/Data/GPA.sav")
str(GPA)
```


## Describe and Plot your data

```{r}
describe(GPA)
```

```{r}
GPA %>% 
# When creating multiple plots at once, long data works best
pivot_longer(cols = colnames(GPA),
             names_to = "variable",
             values_to = "value") %>% 
          ggplot(aes(x  = value)) +
          geom_density() +
# the facet_wrap() fucntion generates plots for each value in "variable"
          facet_wrap(~variable, scales = "free") +
  theme(axis.title.y=element_blank(),
        axis.text.y=element_blank(),
        axis.ticks.y=element_blank(),
        axis.line.y =  element_blank())
```

## Scatterplot

```{r}
ggplot(GPA,
      aes(x = act, y = year1gpa)) +
      geom_point()

```



```{r}


ggplot(GPA,
      aes(x = act, y = year1gpa)) +
      geom_point() +
      geom_smooth(method = "lm", 
                  se = FALSE)
```


```{r}
ggplot(GPA,
      aes(x = act, y = year1gpa)) +
      geom_point() +
      geom_smooth(method = "lm", se = FALSE) +
      geom_smooth(method = "loess", 
                  color = "red",
                  se = FALSE)
```


## Correlation 



$r_{xy} = \frac{ \sum_{i=1}^{n}(x_i-\bar{x})(y_i-\bar{y}) }{\sqrt{\sum_{i=1}^{n}(x_i-\bar{x})^2}\sqrt{\sum_{i=1}^{n}(y_i-\bar{y})^2}}$

```{r}
cov_xy <- sum((GPA$year1gpa - mean(GPA$year1gpa)) * (GPA$act - mean(GPA$act)))
var_x <- sqrt(sum((GPA$year1gpa - mean(GPA$year1gpa))^2))
var_y <- sqrt(sum((GPA$act - mean(GPA$act))^2))

cov_xy/(var_x * var_y)
```


```{r}
cor(GPA$year1gpa, GPA$act)
```


## Significance Test for Correlations


```{r}
corr.test(GPA)
```

```{r}
correlation <- corr.test(GPA)

names(correlation)
```


## `corr_test()` output


```{r}
# correlation matrix

correlation$r
```

```{r}
# p-values

correlation$p
```


```{r}
# confidence intervals

correlation$ci
```


## Running a Regreassion in R


```{r}
reg <- lm(year1gpa ~ act, data = GPA)
summary(reg)
```
## Making Predictions


```{r}
ggplot(GPA,
       aes(x = act, y = year1gpa)) +
  geom_point(alpha = .5, shape = 1) +
  geom_smooth(method = "lm",
              se = FALSE) +
  geom_segment(x = 23,
               xend = 23,
               y = 0,
               yend = coef(reg)[1] + coef(reg)[2]*23,
               linetype = 2) +
    geom_segment(x = 0,
                 xend = 23,
                 y = coef(reg)[1] + coef(reg)[2]*23,
                 yend = coef(reg)[1] + coef(reg)[2]*23,
                 linetype = 2)  +
  geom_segment(x = 27,
               xend = 27,
               y = 0,
               yend = coef(reg)[1] + coef(reg)[2]*27,
               linetype = 2) +
    geom_segment(x = 0,
                 xend = 27,
                 y = coef(reg)[1] + coef(reg)[2]*27,
                 yend = coef(reg)[1] + coef(reg)[2]*27,
                 linetype = 2)   
```


## Making Predictions The "right way"


```{r}
coef(reg)
```



```{r}
as.numeric(coef(reg)[1] + coef(reg)[2]*23)
```


```{r}
# first we create new data that we want to get predictions on
pred_dat <- data.frame("act" = seq(20, 30, by = .01))


# then we use the `predict()` function to make prediction

pred_dat$y_pred <- predict(reg, pred_dat)
str(pred_dat)
```


## Predictions and residuals

```{r}
predictions <- predict(reg)
str(predictions, give.attr = FALSE)
```


```{r}
residuals <- resid(reg)
str(residuals, give.attr = FALSE)


# residuals sum to 0
sum(residuals)
```

## Visualizing Predictions and Residuals

```{r}
# observed data

ggplot(GPA,
 aes(x = act, y = year1gpa)) +
 geom_point(shape = 1) 
```



```{r}
# predictions

ggplot(GPA,
 aes(x = act, y = year1gpa)) +
 geom_point(shape = 1) +
 geom_point(aes(y = predictions), 
                colour = "red")
```




```{r}
# dashed lines (residuals) as distances from predictions and observed values


ggplot(GPA,
 aes(x = act, y = year1gpa)) +
 geom_point(shape = 1) +
 geom_point(aes(y = predictions), 
                color = "red") +
 geom_segment(y = predictions,
              x = GPA$act,
              xend = GPA$act,
              yend = GPA$year1gpa,
              linetype = 2,
              alpha = .5)
```

## Normality of Residuals


```{r}
car::qqPlot(reg, id = FALSE)
```

## Plotting Residuals Against Predictors


```{r}
ggplot(GPA, 
  aes(x = act, y = residuals)) +
  geom_point() +
  geom_hline(yintercept = 0) +
  geom_smooth(method = "loess",
              se = FALSE)
```


## Standardized Regression Output

```{r}
GPA_std <- data.frame(scale(GPA))

reg_std <- lm(year1gpa ~ act, data = GPA_std)
summary(reg_std)
```



## The Meaning of A regression model


```{r}
summary(reg)
```



## Predictable and Unpredictable Parts

```{r}
# generate 5 observations from N~(2, 0)
rnorm(5, mean = 2, sd = 0)
```


```{r}

set.seed(2356)
# add randomness to observations on the left
rnorm(5, mean = 2, sd = 0) + 
rnorm(5, mean = 0, sd = 4) 

# the two lines above are equivalent to just
# set.seed(2356)
# rnorm(5, mean = 2, sd = 4) 

```



## A Visual Representation


```{r}
ggplot(GPA, 
  aes(x = act, y = year1gpa)) +  
  # assume we have no data yet
  geom_point(alpha = 0) +
      geom_smooth(method = "lm", se = FALSE) +
  ylim(0, 5)
```


```{r}
# Generate idealized data

set.seed(7757)

# generate X values
act <- rep(seq(min(GPA$act), max(GPA$act), by =.1), 150)

# the code below generates data according to the regression model

intercept <- coef(reg)[1]
slope <- coef(reg)[2]
residual_var <- sigma(reg)

ideal_data <- rnorm(length(act), mean = intercept + act*slope, sd = sigma(reg))

# plot the idealized data

ggplot(mapping =  aes(x = act, y = ideal_data)) +  
  geom_point(alpha = .2)+
      geom_smooth(method = "lm", se = FALSE) +
  ylim(0, 5)
```


## Statistical Models VS Reality

```{r}
ggplot(mapping =  aes(x = act, y = ideal_data)) +  
  geom_point(alpha = .2)+
      geom_smooth(method = "lm", se = FALSE) +
  ylim(0, 5)
```




```{r}
ggplot(GPA, 
  aes(x = act, y = year1gpa)) +  
  geom_point() +
      geom_smooth(method = "lm", se = FALSE) +
  ylim(0, 5)
```


